﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Csharp结业设计
{
    public partial class Updata_teacher : Form
    {
        private string ConnectionString = "Data Source = LAPTOP-7I3EOIVU\\SQLEXPRESS;" +
       "Initial Catalog=Performance_management_system;Persist Security Info=True; " + "integrated Security = true";
        private SqlConnection conn = null;
        private SqlDataAdapter DataAdapter = null;
        private DataSet dataset = null;

        public Updata_teacher()
        {
            InitializeComponent();
        }

        private void showData2()  //在控件dataGridView1显示数据
        {
            string tname = "";
            try
            {
                if (conn == null) conn.Open();
                DataAdapter = new SqlDataAdapter("SELECT * FROM Teacher_infor", conn);
                dataset = new DataSet();
                DataAdapter.Fill(dataset);
                dataGridView1.DataSource = dataset;
                dataGridView1.DataMember = dataset.Tables[0].ToString();
                tname = dataset.Tables[0].ToString();
                //先清除所有绑定，然后再重新绑定
                textBox1.DataBindings.Clear();
                textBox2.DataBindings.Clear();
                textBox3.DataBindings.Clear();
                textBox4.DataBindings.Clear();
                textBox5.DataBindings.Clear();
                textBox1.DataBindings.Add("Text", dataset, "table.工号"); //数据绑定
                textBox2.DataBindings.Add("Text", dataset, "table.姓名");
                textBox3.DataBindings.Add("Text", dataset, "table.性别");
                textBox4.DataBindings.Add("Text", dataset, "table.职位");
                textBox5.DataBindings.Add("Text", dataset, "table.授课科目");
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommandBuilder builder = new SqlCommandBuilder(DataAdapter);
                int n = DataAdapter.Update(dataset, "Table");
                MessageBox.Show("成功更新数据，有" + n.ToString() + "行受到更新！");
            }
            catch
            {
                MessageBox.Show("更新不成功！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //构造Update语句
            string strSQL = "Update Teacher_infor set ";
            strSQL += "姓名 = '" + textBox2.Text;
            strSQL += "',性别 = '" + textBox3.Text;
            strSQL += "',职位 = '" + textBox4.Text;
            strSQL += "',授课科目 = '" + textBox5.Text+ "'";
            strSQL += " Where 工号 = " + textBox1.Text;
            int index = dataGridView1.CurrentRow.Index; //获取当记录的索引号 
            SqlCommand command = null;
            try
            {
                command = new SqlCommand();
                command.Connection = conn;
                command.CommandText = strSQL;
                conn.Open();
                int n = command.ExecuteNonQuery();		//执行SQL语句
                if (n > 0) MessageBox.Show("成功更新数据，有" + n.ToString() + "行受到更新！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (conn != null) conn.Close();
                command.Dispose();
            }
            showData2();
            //将索引为index的行设置为当前行
            this.dataGridView1.CurrentCell = this.dataGridView1.Rows[index].Cells[0];
            dataGridView1.Rows[index].Selected = true; //加亮显示 
        }

        private void Updata_teacher_Load(object sender, EventArgs e)
        {
            conn = new SqlConnection(ConnectionString);
            textBox1.ReadOnly = true; //不允许修改工号，因为它是主健
            showData2();
        }
    }
}
